package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.domain.db.StatisticsData;
import com.itheima.domain.vo.AnalysisSummaryVo;
import com.itheima.service.db.AnalysisByDayService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AnalysisManager {

    @Reference
    private AnalysisByDayService analysisByDayService;
    /**
     * 查询首页概要统计
     * @return
     */
    public AnalysisSummaryVo summary() {
        return analysisByDayService.summary();
    }

    //数据统计
    public Map statisticsData(String sd, String ed) throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTimeSd1 = null;
        Date dateTimeSd2 = null;
        try {
            dateTimeSd1 = simpleDateFormat.parse(sd);
            dateTimeSd2 = simpleDateFormat.parse(ed);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        //查询今年的周数据
        List<AnalysisByDay> thisYear = analysisByDayService.findDataByType(dateTimeSd1, dateTimeSd2);

        Map<String, List> newUSer = getResultList(thisYear, "newUser");
        Map<String, List> activeUser = getResultList(thisYear, "activeUser");
        Map<String, List> loginUser = getResultList(thisYear, "loginUser");
        Map<String, List> retention = getResultList(thisYear, "retention");

//        long between = DateUtil.between(new Date(sd), new Date(ed), DateUnit.DAY);
//        int time = (int) between;


        //创建集合保存查询到的数据
        Map<String, Map<String,List>> map = new HashMap<>();
        map.put("newUser", newUSer);
        map.put("activeUser", activeUser);
        map.put("loginUser", loginUser);
        map.put("retention", retention);

        return map;
    }

    //获取结果集
    private  Map<String, List> getResultList( List<AnalysisByDay> dataList,String type) {
        //创建返回值集合
        List<String> dateList = new ArrayList<>();
        List<Integer> amountList = new ArrayList<>();
        //对集合进行非空判断
        if (CollectionUtil.isNotEmpty(dataList)) {
            //遍历集合
            for (AnalysisByDay analysisByDay : dataList) {
                //创建StatisticsData对象
                Date recordDate = analysisByDay.getRecordDate();
                String date = new SimpleDateFormat("yyyy-MM-dd").format(recordDate);
                dateList.add(date);
                if (StrUtil.equals(type, "newUser")) {
                    amountList.add(analysisByDay.getNumRegistered());
                } else if (StrUtil.equals(type, "activeUser")) {
                    amountList.add(analysisByDay.getNumActive());
                } else if (StrUtil.equals(type, "loginUser")) {
                    amountList.add(analysisByDay.getNumLogin());
                } else if (StrUtil.equals(type, "retention")) {
                    amountList.add(analysisByDay.getNumRetention1d());
                }
            }
        }
        //创建返回值集合
        Map<String, List> map = new HashMap<>();
        map.put("dates", dateList);
        map.put("datas", amountList);
        return map;
    }

}
